// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/type/cpp_to_type.h>
#include <pollux/type/simple_function_api.h>

namespace kumo::pollux {
    // Recursively check that T and vectorType associate to the same TypeKind.
    template<typename T>
    struct CastTypeChecker {
        static_assert(
            CppToType<T>::maxSubTypes == 0,
            "Complex types should be checked separately.");

        static bool check(const TypePtr &vectorType) {
            return CppToType<T>::typeKind == vectorType->kind();
        }
    };

    template<>
    struct CastTypeChecker<DynamicRow> {
        static bool check(const TypePtr &vectorType) {
            return TypeKind::ROW == vectorType->kind();
        }
    };

    template<typename T>
    struct CastTypeChecker<Generic<T> > {
        static bool check(const TypePtr &) {
            return true;
        }
    };

    template<typename T>
    struct CastTypeChecker<Array<T> > {
        static bool check(const TypePtr &vectorType) {
            return TypeKind::ARRAY == vectorType->kind() &&
                   CastTypeChecker<T>::check(vectorType->childAt(0));
        }
    };

    template<typename K, typename V>
    struct CastTypeChecker<Map<K, V> > {
        static bool check(const TypePtr &vectorType) {
            return TypeKind::MAP == vectorType->kind() &&
                   CastTypeChecker<K>::check(vectorType->childAt(0)) &&
                   CastTypeChecker<V>::check(vectorType->childAt(1));
        }
    };

    template<typename... T>
    struct CastTypeChecker<Row<T...> > {
        static bool check(const TypePtr &vectorType) {
            int index = 0;
            return TypeKind::ROW == vectorType->kind() &&
                   (CastTypeChecker<T>::check(vectorType->childAt(index++)) && ... &&
                       true);
        }
    };
} // namespace kumo::pollux
